package com.pethospital.controller;

import com.pethospital.common.Result;
import com.pethospital.service.AIService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * AI助手控制器
 */
@RestController
@RequestMapping("/ai")
@Slf4j
public class AIController {

    @Resource
    private AIService aiService;

    /**
     * AI助手对话接口
     *
     * @param params 包含问题的参数
     * @return AI助手的回答
     */
    @PostMapping("/chat")
    public Result<Map<String, String>> chat(@RequestBody Map<String, String> params) {
        String question = params.get("question");
        log.info("收到AI对话请求，问题：{}", question);
        
        if (question == null || question.trim().isEmpty()) {
            return Result.error("问题不能为空");
        }
        
        try {
            String answer = aiService.chat(question);
            
            Map<String, String> response = new HashMap<>();
            response.put("answer", answer);
            
            return Result.success(response);
        } catch (Exception e) {
            log.error("AI对话请求处理异常", e);
            return Result.error("AI助手服务暂时不可用，请稍后再试");
        }
    }
} 